
/**
 * 问题提报信息维护
 * @author liuw
 * @date 2019/11/06
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {

        },
        USER_TYPE,
        table_where = {

        },
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field:"ESAADOCNO",title:"问题单号",width:100,hide:true}
            , {field:"ESAA001",title:"客户",hide:true,diabled:true,search:'default',searchOpts: {icon: true,readonly:true}}
            , {field:"ESAA001_NAME",title:"客户简称",width:100}
            , {field:"ESAA002",title:"提单人员",width:100,search:"default"}
            , {field:"ESAA003",title:"问题类型",width:100,
                filterDatas: app.datas_('problem_esai100_esaa003'),search:'default', searchOpts: {type: 'select'}}
            , {field:"ESAASTUS",title:"问题状态",width:100,
                filterDatas: app.datas_('problem_esai100_esaastus')}
            , {field:"ESAA004",title:"提单日期",width:100,sort:true,search:"default",searchOpts:{type:'date'}}
            , {field:"ESAA005_NAME",title:"产品线",width:130}
            , {field:"ESAA006",title:"ERP产品",width:100,
                filterDatas: app.datas_('problem_esai100_esaa006')}
            , {field:"ESAA007",title:"程序名称",width:100}
            , {field:"ESAA008",title:"问题描述",width:100}
            , {field:"ESAA010",title:"处理人员",width:100,hide:true,search:'default', searchOpts: {icon: true,readonly: true}}
            , {field:"ESAA010_NAME",title:"处理人员",width:100}
            , {field:"userType",title:"用户类型",width:100,hide:true}
            , {
                width: 60,
                title: '',
                toolbar: '#script-esai100setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        esai100SettingAction,
        moduleName = '问题',
        editArea = ['480px', '430px']
    ;

    var esai100 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'esai100',
                _searchs: [{
                    id: 'esai100', tableId: 'esai100_table',
                    datas: _searchsArr.pushArray([{
                        field: 'ESAASTUS', width: 60, title: '状态码', search: 'default',
                        searchOpts: {
                            type: 'selectmultiple',
                            colX: 2,
                            default: ['1','2','3'],
                            data: app.datas_('problem_esai100_esaastus')
                        }
                    }])
                }],
                _loadTplsFull: [{
                    dom: 'esai100_edit', tpl: 'esai100_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'esai100_table'),
                    url: g_ctxPath + 'crm/esai100/getList',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    method: 'post',
                    id: 'esai100_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: false,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-esai100setting-action').innerHTML).render({}, function (string) {
                    esai100SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(esai100_table)', function (obj) {
                    console.log(obj.data);

                    layer.tips(esai100SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'esai100_edit',
                                            tagData: {
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                maxmin:true
                                            }
                                        });
                                        break;
                                    case '2':
                                        app.confirm({
                                            msg: '是否删除选择的数据？',
                                            ok: function () {
                                                self.deleteData(obj);
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(esai100_table)', function (obj) {
                    self._openTplFull({
                        tag: 'esai100_edit',
                        tagData: {
                            operation:'detail',
                            obj : obj,
                            moduleName : moduleName
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(esai100_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'esai100_edit',
                                tagData: {
                                    is_add : true,
                                    moduleName : moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;

                        case 'accept':
                            app.confirm({
                                msg: '是否受理问题提单？',
                                ok: function () {
                                    self.changeState("accept");
                                }
                            });
                            break;
                        case 'finish':
                            app.confirm({
                                msg: '是否确认问题处理已完成？',
                                ok: function () {
                                    self.changeState("finish");
                                }
                            });
                            break;
                        case 'close':
                            app.confirm({
                                msg: '是否确认问题处理已结案？',
                                ok: function () {
                                    self.changeState("close");
                                }
                            });
                            break;
                        case 'revoke':
                            app.confirm({
                                msg: '是否确认该问题处理未通过验证？',
                                ok: function () {
                                    self.changeState("revoke");
                                }
                            });
                            break;
                        case 'transfer':
                            app.confirm({
                                msg: '是否确认要转派处理人员',
                                ok: function () {
                                    self.transfer();
                                }
                            });
                            break;

                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'esai100_table', where: table_where});
        },

        changeState: function(action)  {
            var checkStatus = table.checkStatus('esai100_table');
            var flag = true;
            if(checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
                return;
            }
            if(checkStatus.data.length > 1 ) {
                app.showWarning("只能选择一个问题进行状态的修改");
                return;
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    let _temp = app.fields_('ESAA_T');
                    switch (action) {
                        case "accept" :
                            if(v.ESAASTUS === "1"){
                                v.ESAASTUS = "2"
                            }
                            else {
                                app.showWarning("未处理的问题才能受理");
                                flag = false;
                                return;
                            }
                            break;
                        case "finish" :
                            if(v.ESAASTUS === "2"){
                                v.ESAASTUS = "3"
                            }
                            else {
                                app.showWarning("处理中的问题才能完成");
                                flag = false;
                                return;
                            }
                            break;
                        case "close" :
                            v.ESAASTUS = "4";
                            break;
                        case "revoke" :
                            if(v.ESAASTUS === "3"){
                                v.ESAASTUS = "1"
                            }
                            else {
                                app.showWarning("待验证的问题才能退回");
                                flag = false;
                                return;
                            }
                            break;
                    }
                    _xdata.push(v);
                });
                if(!flag){
                    return;
                }else {
                    app.ajax({
                        xurl: 'crm/esai100/changeStatus',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName +'状态修改成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },
        transfer: function() {
            var checkStatus = table.checkStatus('esai100_table');
            if (checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            let _xdata = [];
            if (checkStatus.data.length > 0) {
                let canTransfer = true;
                checkStatus.data.forEach(function (v) {
                    if(v.ESAASTUS === '4') {
                        canTransfer = false;
                        return;
                    }
                    _xdata.push(v);
                });
                if(!canTransfer) {
                    app.showWarning("只有未结案的问题才能转派");
                    return;
                }

                self._openTplFull({
                    tag: 'common_open_window',
                    tagData: {
                        config: {
                            method:'q_sysuser_id',
                            searchParam: {
                                org_permission: true
                            },
                            columnList: [
                                {label: '人员编号', prop: 'ID', width: '150'},
                                {label: '姓名', prop: 'NAME', width: '150'},
                                {label: '账户', prop: 'ACCOUNT', width: '150'}
                            ]
                        },
                        cb: function (data) {
                            _xdata.forEach(v => v.ESAA010 = data.ID);
                            app.ajax({
                                xurl: 'crm/esai100/transferBatch',
                                xdata: _xdata,
                                loading: true,
                                toJsonStr: true,
                                okmsg: '问题处理人员转派成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        self.reloadTableCurr();
                                    }
                                }
                            });
                        },
                    },
                    index: 1,
                    openOpts: {
                        title: '选择处理人员',
                        area: ['600px', '470px']
                    }
                });

            }
        },
        deleteData: function (obj) {
            if(obj.data.ESAASTUS !== '4'){
                app.showWarning("只有已结案的问题才能删除");
                return;
            }
            app.ajax({
                xurl: 'crm/esai100/del',
                xdata: obj.data,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            var checkStatus = table.checkStatus('esai100_table');
            if (checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            if (checkStatus.data.length > 0) {
                let _xdata = [];
                let canDelete = true;
                checkStatus.data.forEach(function (v) {
                    if(v.ESAASTUS !== '1') {
                        canDelete = false;
                        return;
                    }
                    var _temp = app.fields_('ESAA_T');
                    _temp.ESAADOCNO = v.ESAADOCNO;
                    _xdata.push(_temp);
                });
                if(!canDelete) {
                    app.showWarning("只有未处理的问题才能删除");
                    return;
                }
                app.ajax({
                    xurl: 'crm/esai100/delBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName +'信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },
        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'esai100',
                    methods: {
                        handleIconClick: function (field) {

                            var vue = this;
                            if (field === 'ESAA001') {
                                if(USER_TYPE === '1') {
                                    app.showWarning("您没有权限进行客户开窗");
                                    return;
                                }
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.ESAA001 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field === 'ESAA010') {
                                if(USER_TYPE === '1'){
                                    self._openTplFull({
                                        tag: 'common_open_window',
                                        tagData: {
                                            config: {
                                                method:'q_esaa010',
                                                columnList: [
                                                    {label: '人员编号', prop: 'ESAA010', width: '150'},
                                                    {label: '姓名', prop: 'NAME', width: '150'},
                                                    {label: '账户', prop: 'ACCOUNT', width: '150'}
                                                ]
                                            },
                                            cb: function (data) {
                                                vue.isearch.fields.ESAA010 = data.ESAA010;
                                            },
                                        },
                                        index: 1,
                                        openOpts: {
                                            title: '选择处理人员',
                                            area: ['600px', '470px']
                                        }
                                    });
                                }else {
                                    self._openTplFull({
                                        tag: 'common_open_window',
                                        tagData: {
                                            config: {
                                                method:'q_sysuser_id',
                                                columnList: [
                                                    {label: '人员编号', prop: 'ID', width: '150'},
                                                    {label: '姓名', prop: 'NAME', width: '150'},
                                                    {label: '账户', prop: 'ACCOUNT', width: '150'}
                                                ]
                                            },
                                            cb: function (data) {
                                                vue.isearch.fields.ESAA010 = data.ID;
                                            },
                                        },
                                        index: 1,
                                        openOpts: {
                                            title: '选择处理人员',
                                            area: ['600px', '470px']
                                        }
                                    });
                                }
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.ESAASTUS': function (newValue, oldValue) {
                            var data = '';
                            if(newValue.length === 0) {
                                data = '';
                            }else {
                                data = new Array(newValue).join(",");
                            }
                            this.isearch.fields.ESAASTUS_LIST = data;
                        }
                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
            // 获取当前用户默认数据
            app.ajax({
                xurl: 'crm/esai100/getUserInfo',
                cb: function (result) {
                    if (result.ok) {
                        let data = result.data.data;
                        USER_TYPE = data.USER_TYPE;
                    }
                }
            });
        }
    });
    return new esai100();
});
